<?php
declare (strict_types=1);

namespace app\user\model;

use app\common\lib\Identity;
use think\Exception;
use think\facade\Cache;
use think\Model;

/**
 * @mixin \think\Model
 * @property int $id
 * @property string $username
 * @property string $nickname
 * @property string $password
 * @property string $email
 * @property string $mobile
 * @property string $status
 * @property string $appid
 * @property string $secret
 * @property string $money
 */
class User extends \app\common\model\User
{
    public function register($data)
    {
        $data['appid'] = Identity::appid();
        $data['secret'] = Identity::secret($data['appid']);
        if (self::create($data)) {
            return true;
        }
        throw new Exception('注册失败');
    }

    public function login(string $username, string $password, bool $rememberme = false, bool $api = false)
    {
        $user = $this->where('username|mobile|email', $username)->findOrEmpty();

        if ($user->isEmpty()) {
            throw new Exception('用户不存在');
        }

        if (!password_verify($password, $user->password)) {
            throw new Exception('用户账号或者密码错误');
        }

        if (self::NORMAL !== $user->status && $api) {
            throw new Exception('用户已被禁用');
        }

        $user->hidden(['password']);
        if (!$api) {
            if ($rememberme) {
                cookie('username', $user->username, 86400 * 7);
                cookie('uid', $user->id, 86400 * 7);
            } else {
                cookie('username', $user->username);
                cookie('uid', $user->id);
            }
            session('user', $user);
            return [
                'url' => '\user\user\ucenter'
            ];
        } else {
            $accessToken = Identity::accessToken($user->appid, $user->secret);
            return $accessToken;
        }

    }

    public function email(string $email = '')
    {
        $user = self::where('email', $email)->findOrEmpty();

        if ($user->isEmpty()) {
            $appid = Identity::appid();
            $secret = Identity::secret($appid);
            $data = [
                'email'    => $email,
                'appid'    => $appid,
                'secret'   => $secret,
                'nickname' => $email,
                'username' => $email,
                'password' => password_hash($email . '123456789', PASSWORD_DEFAULT),
                'status'   => self::NORMAL
            ];

            $user = self::create($data);
        }
        $user->hidden(['password']);
        cookie('email', $email);
        cookie('username', $user->username);
        cookie('nickname', $user->nickname);
        cookie('uid', $user->id);
        session('user', $user);
        return [
            'url' => '\user\user\ucenter',
        ];
    }

    public function resetSecret()
    {

        $secret = Identity::secret($this->appid);
        if(time() - $this->last_update_secret_time < 86400*7){
            throw new Exception('7天内只能重置一次');
        }
        $this->secret = $secret;
        $this->last_update_secret_time = time();
        if ($this->save()) {
            return [
                'secret' => $secret
            ];
        };

        throw new Exception('重置密匙失败');
    }

    public static function modifyIpList($userId,$ip){
        if(self::update(['ip_list'=>$ip],['id'=>$userId])){
            return true;
        }
        throw new Exception('ip白名单设置失败');
    }
}
